package cc.linker.steplink.crm.merchanth5.controller;

import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.service.ExerciseService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/h5/exercise")
public class ExerciseController {


    @Autowired
    private ExerciseService exerciseService;

    @Autowired
    private RequestFilter requestFilter;

//    @GetMapping("/getexercise")
//    public List<Map<String,Object>> getExercise(HttpServletRequest request,
//                                                @RequestParam(value = "goodsId", defaultValue = "") int goodsId,
//                                                @RequestParam(value = "customerId", defaultValue = "", required = false) String customerId){
//        WxUser wxUser =  requestFilter.getWxUser(request);
//        String orgId = wxUser.getOrgId();
//        if (StringUtils.isEmpty(customerId)) {
//            customerId = orgId;
//        }
//        Map<String,Object> param = new HashMap<>();
//        param.put("customerId",customerId);
//        param.put("orgId",orgId);
//        param.put("goodsId",goodsId);
//        return exerciseService.getExercise(param);
//    }

    @PostMapping("/getexercisebyorder")
    public List<Map<String,Object>> getExerciseByOrder(@RequestBody Map<String, Object> param,HttpServletRequest request){
        if(StringUtils.isEmpty(param.get("orderSn")) && StringUtils.isEmpty(param.get("orderId"))){
            throw GlobalException.build("参数错误");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        param.put("orgId",orgId);
        return exerciseService.getExerciseByOrder(param);
    }

    @PostMapping("/getexercisebygoods")
    public List<Map<String,Object>> getExerciseByGoods(@RequestBody Map<String, Object> param,HttpServletRequest request){
        if(StringUtils.isEmpty(param.get("goodsId"))){
            throw GlobalException.build("参数错误");
        }
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        param.put("orgId",orgId);
        return exerciseService.getExerciseByGoods(param);
    }
}
